//
//  HXSelectDateAlert.m
//  walrusWuLiuCP
//
//  Created by 海象 on 15/9/28.
//  Copyright (c) 2015年 海象. All rights reserved.
//

#import "HXSelectDateAlert.h"
#import "AppDelegate.h"

#define totalHeight  240

@implementation HXSelectDateAlert

- (id)initWithFrame:(CGRect)frame selectDateStr:(NSString*)dateStr{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds=YES;
        self.backgroundColor=[UIColor whiteColor];
        self.layer.cornerRadius=5;
        self.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, totalHeight);

        NSArray*array=@[@"年",@"月",@"日"];
        int width=self.frame.size.width/3;
        
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 35)];
        
        headView.layer.masksToBounds = YES;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:headView.bounds byRoundingCorners:UIRectCornerTopRight| UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];//指定圆角位置 大小
        
        CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];
        masklayer.frame = headView.bounds;
        masklayer.path = path.CGPath;
        headView.layer.mask = masklayer;
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = headView.bounds;
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[UIColor colorWithRed:66/255.0 green:80/255.0 blue:225/255.0 alpha:1.0].CGColor,
                           (id)[UIColor colorWithRed:81/255.0 green:78/255.0 blue:215/255.0 alpha:1.0].CGColor,
                           (id)[UIColor colorWithRed:96/255.0 green:78/255.0 blue:207/255.0 alpha:1.0].CGColor, nil];
        [headView.layer addSublayer:gradient];
        [self addSubview:headView];
        UILabel * titleL = [[UILabel alloc]initWithFrame:CGRectMake((ViewFrame_W(headView) - 100)/2, 5, 100, headView.frame.size.height - 10)];
        titleL.backgroundColor = [UIColor clearColor];
        titleL.textAlignment = NSTextAlignmentCenter;
        titleL.text = @"录像时间";
        titleL.textColor = [UIColor whiteColor];
        titleL.font = [UIFont systemFontOfSize:18.0];
        [headView addSubview:titleL];
//        for(int i=0;i<3;i++){
//            UILabel*label=[[UILabel alloc] initWithFrame:CGRectMake(i*width,10, width, 21)];
//            label.text=[array objectAtIndex:i];
//            label.textAlignment=NSTextAlignmentCenter;
//            label.font=[UIFont boldSystemFontOfSize:14.f];
//            [self addSubview:label];
//        }
        
        UIImageView*lineImgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 35, self.frame.size.width, 1)];
        lineImgView.image=[UIImage imageNamed:@"dottedLine.png"];
        [self addSubview:lineImgView];

        _datePicker=[[UIDatePicker alloc] init];
        _datePicker.frame=CGRectMake(0, ViewFrameY_H(lineImgView)+10, self.frame.size.width,165);
        _datePicker.datePickerMode=UIDatePickerModeDate;
        NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        _datePicker.locale=locale;
        _datePicker.maximumDate=[NSDate date];
        if (!dateStr.length)
            dateStr=[[NSDate date] toStringByChineseDateLine];
        
        NSString*str=[NSString stringWithFormat:@"%@",dateStr];
        NSDateFormatter*formatter=[[NSDateFormatter alloc] init];
        [formatter setLocale:locale];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        _datePicker.date=[formatter dateFromString:str];
        [self addSubview:_datePicker];

        
        UIImageView*lineImgView2=[[UIImageView alloc] initWithFrame:CGRectMake(0,ViewFrameY_H(_datePicker)+10, self.frame.size.width, 1)];
        lineImgView2.image=[UIImage imageNamed:@"dottedLine.png"];
        [self addSubview:lineImgView2];

        UIButton*confirmBtn=[[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-(self.frame.size.width*0.5)/2,ViewFrameY_H(lineImgView2)+10, self.frame.size.width*0.5, 30)];
        confirmBtn.backgroundColor=LightBlueColor;
        [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
        confirmBtn.titleLabel.font=[UIFont systemFontOfSize:15.f];;
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        confirmBtn.layer.cornerRadius=confirmBtn.frame.size.height/2;
        [confirmBtn addTarget:self action:@selector(confirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:confirmBtn];
        CGRect rect=self.frame;
        rect.size.height=ViewFrameY_H(confirmBtn)+10;
        self.frame=rect;
    }
    return self;
}

//确定点击
-(void)confirmBtnClicked:(id)sender{
    NSString* dateStr=[_datePicker.date toStringByChineseDateLine];

    NSArray *array = [dateStr componentsSeparatedByString:@"-"];
    if ([_Cudelegate respondsToSelector:@selector(selectDateStr:Date:)]) {
                [_Cudelegate selectDateStr:dateStr Date:_datePicker.date];
        }
            [self alertRemoveFromSuperview];

}


-(void)show{
    
    _bgView=[[UIControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _bgView.backgroundColor=[UIColor lightGrayColor];
    _bgView.alpha=0.8;
    
//    self.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [_bgView addTarget:self action:@selector(alertRemoveFromSuperview) forControlEvents:UIControlEventTouchUpInside];
}
-(void)alertRemoveFromSuperview
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.alpha=0;
    _bgView.alpha=0;
    [UIView commitAnimations];
    [_bgView removeFromSuperview];
    [self removeFromSuperview];
    
}

@end
